# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Hurricane Pootis <hurricanepootis@protonmail.com>
# Contributor: John Sowiak <john@archlinux.org>
# Contributor: tobias <tobias@archlinux.org>

# For legal reasons, we can't separately package the Optix headers so we'll
# just build the package against them. I checked with NVIDIA and this way is
# fine with them.

pkgname=blender
pkgver=4.0.1
pkgrel=5
epoch=17
pkgdesc="A fully integrated 3D graphics creation suite"
arch=('loong64' 'x86_64')
license=('GPL')
url="https://www.blender.org"
depends=('libpng' 'libtiff' 'openexr' 'python' 'desktop-file-utils' 'python-requests' 'potrace'
         'shared-mime-info' 'hicolor-icon-theme' 'xdg-utils' 'glew' 'openjpeg2' 'python-numpy'
         'freetype2' 'openal' 'ffmpeg' 'fftw' 'boost-libs' 'opencollada' 'alembic' 'openxr'
         'openimageio' 'libsndfile' 'jack' 'opencolorio' 'openimagedenoise' 'materialx'
         'jemalloc' 'libspnav' 'ptex' 'opensubdiv' 'openvdb' 'sdl2' 'embree' 'libharu'
         'draco' 'openpgl' 'level-zero-loader' 'libxkbcommon' 'libepoxy' 'usd' 'openshadinglanguage'
         'intel-oneapi-compiler-shared-runtime-libs' 'intel-oneapi-compiler-dpcpp-cpp-runtime-libs')
makedepends=('cmake' 'boost' 'mesa' 'git' 'svn' 'llvm' 'cuda' 'ninja' 'wayland-protocols' 'libxkbcommon'
             'libdecor' 'hip-runtime-amd' 'level-zero-headers' 'intel-oneapi-dpcpp-cpp'
             'intel-oneapi-compiler-shared-runtime' 'intel-compute-runtime')
optdepends=('cuda: Cycles renderer CUDA support'
            'intel-compute-runtime: Cycles renderer Intel OneAPI support'
            'libdecor: wayland support')
# We're using !buildflags here as otherwise we get stuff such as FS#77557
# It'd be better to figure out exactly which of the flags is the culprit which I attempted but without any success.
options=('!buildflags')
source=("git+https://projects.blender.org/blender/blender.git#tag=v$pkgver"
        "git+https://projects.blender.org/blender/blender-addons.git#tag=v$pkgver"
        "git+https://projects.blender.org/blender/blender-addons-contrib#tag=v$pkgver"
        https://developer.download.nvidia.com/redist/optix/v7.7/OptiX-7.7-Include.zip
        https://projects.blender.org/blender/blender/pulls/115127.patch
        force-draco1.patch
        force-draco2.patch)
sha512sums=('SKIP'
            'SKIP'
            'SKIP'
            '55caea5beccdc0adebdba8a825f3cfee083015311de5602cfcf215139ccbf9fed62f10ee1dcdf7e31a3557aabbc1fa872eeed67bc46f59fb4e5abd18331c0aca'
            'ae4aa976f00184ccef735b58734ba13bebafa5ebb64b86d784528c60a4d910065246beca00b0fcf97aed4ce14eeefb29f7be53d76d65df9e122f11a8c1da6818'
            'e239da4f3906f1b54265435181cf770bae3d269c8d915df9a73861e6ee71ec70bf2339426e7c81a91e5a567273b3b3742d7a99feefd3398d821b26e1ff3a56d0'
            'c162646eba42ea7112cfb615466c2cbaddac8d254d4a9e91cf887c5bffef1325a983701163223a1fdee091310ac5574a400347aab7f52321825b5fa3d5a9036f')

prepare() {
  cd "$pkgname"

  cp -r "$srcdir"/blender-addons scripts/addons
  cp -r "$srcdir"/blender-addons-contrib scripts/addons_contrib

  # fix draco
  patch -p1 -i "$srcdir"/force-draco1.patch
  patch -p1 -d scripts/addons -i "$srcdir"/force-draco2.patch

  # To fix manpage installation
  # https://devtalk.blender.org/t/regression-cmake-install-error-for-man-page/32129
  # https://projects.blender.org/blender/blender/pulls/115127
  patch -Np1 -i "$srcdir"/115127.patch

  # Revert https://projects.blender.org/blender/blender/commit/4b891b4af which causes all
  # CMake custom commands to be run in serial instead of in parallel.
  sed -i "s/USES_TERMINAL//g" intern/cycles/kernel/CMakeLists.txt
}

get_pyver() {
  python -c 'import sys; print(str(sys.version_info[0]) + "." + str(sys.version_info[1]))'
}

build() {
  cd "$pkgname"

  local PYTHON_VER="$(get_pyver)"

  # CUDA arch 90 is unsupported
  echo 'set(CYCLES_CUDA_BINARIES_ARCH sm_52 sm_53 sm_60 sm_61 sm_62 sm_70 sm_72 sm_75 sm_80 sm_86 sm_87 sm_89 compute_89 CACHE STRING "CUDA architectures to build binaries for" FORCE)' > precache
  echo 'mark_as_advanced(CYCLES_CUDA_BINARIES_ARCH)' >> precache

  # Disable manpage for the time being
  cmake \
    -Bbuild \
    -GNinja \
    -Cbuild_files/cmake/config/blender_release.cmake \
    -C precache \
    -DCUDA_HOST_COMPILER=/usr/bin/gcc-12 \
    -DLLVM_VERSION=16 \
    -DPYTHON_VERSION=$PYTHON_VER \
    -DHIP_ROOT_DIR=/opt/rocm \
    -DOPTIX_ROOT_DIR="$srcdir" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_DOC_MANPAGE=OFF \
    -DOCLOC_INSTALL_DIR=/usr \
    -DUSD_ROOT_DIR=/usr \
    -DSYCL_OFFLINE_COMPILER_PARALLEL_JOBS=8
  cmake --build build
    # For debug:
    # -DSYCL_CPP_FLAGS=--verbose \
}

package() {
  cd "$pkgname"

  DESTDIR="${pkgdir}" cmake --install build
  install -Dm755 release/bin/blender-softwaregl "${pkgdir}/usr/bin/blender-softwaregl"
  python -m compileall "${pkgdir}/usr/share/blender"
  python -O -m compileall "${pkgdir}/usr/share/blender"

  install -Dm644 release/freedesktop/org.blender.Blender.metainfo.xml "${pkgdir}/usr/share/metainfo/org.blender.Blender.metainfo.xml"

  # Manually install additional scripts (See FS#69351)
  cp -r scripts/addons_contrib/* "${pkgdir}"/usr/share/blender/4*/scripts/addons_contrib/

  # Manually install draco bindings (See FS#73415)
  mkdir -p "${pkgdir}/usr/lib/python$(get_pyver)"/
  mv "${pkgdir}"/usr/share/blender/4*/python/lib/* "${pkgdir}"/usr/lib/
  rm -r "${pkgdir}"/usr/share/blender/4*/python

  # Move OneAPI AOT lib to proper place
  mv "${pkgdir}"/usr/share/blender/lib/libcycles_kernel_oneapi_aot.so "${pkgdir}"/usr/lib/
}
